

// ignore: import_of_legacy_library_into_null_safe
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_pleasure_video/constant/color.dart';
import 'package:flutter_pleasure_video/data/model/video_model.dart';
import 'package:flutter_pleasure_video/util/click_util.dart';
import 'package:flutter_pleasure_video/util/extensions/string_extensions.dart';
import 'package:get/get.dart';

// ignore: must_be_immutable
class ComplicatedImageSlider extends StatelessWidget {
  final List<VideoModel>? videoList;
  final Function(VideoModel)? onClickItemListener;
  final Function(Function(bool isPlay)) isPlayListener;
  final CarouselOptionsController _optionsController=CarouselOptionsController();

  ComplicatedImageSlider(this.videoList,this.onClickItemListener,this.isPlayListener){
    isPlayListener(_optionsController.setAutoPlay);
  }


  @override
  Widget build(BuildContext context) {
    if(videoList==null){
      return Container();
    }
    return  Container(
        child: Obx(
            ()=>CarouselSlider(
              options: CarouselOptions(
                autoPlay: _optionsController.autoPlay.value,
                aspectRatio: 2.0,
                enlargeCenterPage: true,
              ),
              items: _getImageSliders(videoList!),
            ),
        ),
    );
  }


  List<Widget> _getImageSliders(List<VideoModel> videoList){
    List<Widget> arrayWidget=[];
    for(int i=0;i<videoList.length;i++){
      arrayWidget.add(getSlidersItem(videoList[i], i));
    }
    return arrayWidget;
  }

  Widget getSlidersItem(VideoModel model,int position){
    return GestureDetector(
      onTap: (){
        if(ClickUtil.isFastClick()){
          return;
        }
        if(onClickItemListener!=null){
          onClickItemListener!(model);
        }
        "点击了第$position个，title:${model.title}".print;
      },
      child: Container(
        child: Container(
          margin: EdgeInsets.all(5.0),
          child: ClipRRect(
              borderRadius: BorderRadius.all(Radius.circular(5.0)),
              child: Stack(
                children: <Widget>[
                  Image.network(model.cover??"", fit: BoxFit.cover, width: 1000.0),
                  Positioned(
                    bottom: 0.0,
                    left: 0.0,
                    right: 0.0,
                    child: Container(
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                          colors: [
                            Color.fromARGB(200, 0, 0, 0),
                            Color.fromARGB(0, 0, 0, 0)
                          ],
                          begin: Alignment.bottomCenter,
                          end: Alignment.topCenter,
                        ),
                      ),
                      padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
                      child: Text(
                        model.title??"",
                        style: TextStyle(
                          color: AppColor.white,
                          fontSize: 20.0,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  ),
                ],
              )
          ),
        ),
      ),
    );
  }
}

class CarouselOptionsController extends GetxController{
  var autoPlay=true.obs;

  setAutoPlay(bool autoPlay)=>this.autoPlay.value=autoPlay;
}